/**
 *
 */
package reuseplugin.login;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import reuseplugin.Activator;
import reuseplugin.ClientInterface;

/**
 * @author Julius Ollesch
 * 
 */
public class LoginDialog implements ClientInterface {

	public static void main(String[] args) {
		new LoginDialog();

	}

	private Shell sShell = null; // @jve:decl-index=0:visual-constraint="0,0"
	private Text textBenutzername = null;
	private Label labelBenutzername = null;
	private Label labelPasswort = null;
	private Text textPasswort = null;
	private Button buttonOk = null;
	private Button buttonCancel = null;
	private Button checkBoxAngemeldtBleiben = null;

	public boolean open = false;

	/**
	 * This method initializes sShell
	 */

	public LoginDialog() {
		if (!leseBenutzer()) {
			Display display = erzeugeGUI();
			sShell.open();
			this.open = true;
			while (!sShell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
			// display.dispose();
			this.open = false;
		}
	}

	public LoginDialog(Boolean speichern) {

		Display display = erzeugeGUI();
		if (speichern) {
			checkBoxAngemeldtBleiben.setSelection(true);
			checkBoxAngemeldtBleiben.setEnabled(false);
		}
		sShell.open();
		this.open = true;
		while (!sShell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		// display.dispose();
		this.open = false;

	}

	private void close() {
		sShell.dispose();
	}

	private Display erzeugeGUI() {
		GridData gridData3 = new GridData();
		gridData3.grabExcessHorizontalSpace = true;
		gridData3.horizontalAlignment = GridData.FILL;
		gridData3.verticalAlignment = GridData.CENTER;
		gridData3.grabExcessVerticalSpace = true;
		GridData gridData2 = new GridData();
		gridData2.horizontalAlignment = GridData.FILL;
		gridData2.grabExcessHorizontalSpace = true;
		gridData2.grabExcessVerticalSpace = true;
		gridData2.verticalAlignment = GridData.CENTER;
		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = GridData.FILL;
		gridData1.horizontalSpan = 2;
		gridData1.verticalAlignment = GridData.CENTER;
		GridData gridData = new GridData();
		gridData.horizontalSpan = 2;
		gridData.verticalAlignment = GridData.CENTER;
		gridData.horizontalAlignment = GridData.FILL;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		gridLayout.makeColumnsEqualWidth = true;
		Display display = Display.getCurrent();

		if (display == null) {
			display = new Display();
		}

		sShell = new Shell(display, SWT.DIALOG_TRIM | SWT.SYSTEM_MODAL);
		sShell.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(
				ISharedImages.IMG_TOOL_NEW_WIZARD));

		sShell.setText(Messages.LoginDialog_titleLogin);
		sShell.setLayout(gridLayout);
		sShell.setSize(new Point(373, 129));
		labelBenutzername = new Label(sShell, SWT.NONE);
		labelBenutzername.setText(Messages.LoginDialog_labelUsername);
		textBenutzername = new Text(sShell, SWT.BORDER);
		textBenutzername.setLayoutData(gridData);
		labelPasswort = new Label(sShell, SWT.NONE);
		labelPasswort.setText(Messages.LoginDialog_labelPassword);
		textPasswort = new Text(sShell, SWT.BORDER | SWT.PASSWORD);
		textPasswort.setLayoutData(gridData1);
		Label filler = new Label(sShell, SWT.NONE);
		checkBoxAngemeldtBleiben = new Button(sShell, SWT.CHECK);
		checkBoxAngemeldtBleiben.setText(Messages.LoginDialog_labelRemember);
		Label filler2 = new Label(sShell, SWT.NONE);
		Label filler1 = new Label(sShell, SWT.NONE);
		buttonOk = new Button(sShell, SWT.NONE);
		buttonOk.setText(Messages.LoginDialog_labelLogin);
		buttonOk.setLayoutData(gridData2);
		buttonOk
				.addTraverseListener(new org.eclipse.swt.events.TraverseListener() {
					public void keyTraversed(
							org.eclipse.swt.events.TraverseEvent e) {
						ok();

					}
				});
		buttonOk.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			@Override
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
				ok();

			}
		});
		buttonCancel = new Button(sShell, SWT.NONE);
		buttonCancel.setText(Messages.LoginDialog_labelCancel);
		buttonCancel.setLayoutData(gridData3);
		buttonCancel
				.addTraverseListener(new org.eclipse.swt.events.TraverseListener() {
					public void keyTraversed(
							org.eclipse.swt.events.TraverseEvent e) {
						close();
					}
				});
		buttonCancel
				.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
					@Override
					public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
						close();
					}
				});
		return display;

	}

	public boolean leseBenutzer() {
		String benutzername = Activator.getDefault().getPreferenceStore()
				.getString("userPreference"); //$NON-NLS-1$
		String passwort = Activator.getDefault().getPreferenceStore()
				.getString("passwordPreference"); //$NON-NLS-1$
		return c.login(benutzername, passwort);
	}

	private void ok() {
		String benutzername = textBenutzername.getText();
		String passwort = textPasswort.getText();
		if (c.login(benutzername, passwort)) {
			if (checkBoxAngemeldtBleiben.getSelection()) {
				if (!speichereBenutzer()) {
					MessageDialog.openError(sShell,
							Messages.LoginDialog_titleErrorSaveFailed,
							Messages.LoginDialog_errorSaveFailed);
				}
			}
			close();
		} else {
			MessageDialog.openError(sShell,
					Messages.LoginDialog_titleLoginFailed,
					Messages.LoginDialog_loginFailed);
		}
	}

	public boolean speichereBenutzer() {
		if (c.isAktiv()) {
			Activator.getDefault().getPreferenceStore().setValue(
					"userPreference", c.getBenutzer().getBenutzername()); //$NON-NLS-1$
			Activator.getDefault().getPreferenceStore().setValue(
					"passwordPreference", c.getBenutzer().getPasswort()); //$NON-NLS-1$

			return true;
		}
		return false;

	}

}
